/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
// module
import Taro, { Component } from '@tarojs/taro'
import { Swiper, Image, Video } from '@tarojs/components'

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http'

// 引用组件
import { Addcbtn, Cbtn, Chead, Destop, Editcunit, Ibottom, Ibottomp, Listunitone, Listunittwo, LogoLine, LogoLinep, Tabheader } from '../../project/comp';

/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class ArticleDetail extends Component {

	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
            // 获取屏幕高度
            scrollHeight: wx.getSystemInfoSync().windowHeight,
            // 判断是否为管理员
            is_master: false,
            // 课堂列表
            class: []
		}
    }

    /*------------------------------------------------------------------------------------
	 * 生命周期
	 *-----------------------------------------------------------------------------------*/
    // didmount
    componentDidMount(){
        this.getData()
    }

    // didshow
    componentDidShow(){
        this.getData()
    }

    getData = (callbcak)=> {
        Http.AuthOk((res)=>{
            this.setState({
                is_master: res
            })
        })
        Http.Loading({
            url:'class/list.php',
            success: (data)=>{
                console.log('0000',data)
                this.setState({
                    class: data
                },()=>{
                    console.log('++++++++++++++++++++++++++++++++------------------',this.state.class)
                })
                if(callbcak){
                    callbcak();
                }	
            }
        })
    }
    
    /*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/
    // onRedirect
	onRedirect(url){
		wx.redirectTo({
			url: url
		})
	}	

	// onNavigate
	onNavigate(url){
		wx.navigateTo({
			url: url
		})
	}

	//onNavigateBack
	onNavigateBack(num=1){
		wx.navigateBack({
			delta: num
		})                    
    }
    
    // 点赞接口
    doit = (type, item) =>{
        Http.post({
            url:'/good/logic/add.php',
            data:{
                count_good:item.count_like,
                id:item.id,
                type:type,
            },
            success:(res)=>{
                this.getData(()=>{
                    Toast.success('点赞成功');
                });
            }
        })	
    }

	//点赞
	onGoodClick = (type, item)=> {
		
		Storage.get({
			key: type +'good',
			success:(res)=>{
                console.log('------------------------------------------------',item)
				if(res){
					console.log(res)
					if(res.indexOf(item.id) != -1){
						Toast.error('请勿重复点赞');
					}else{
						this.doit(type, item);
						let arr = res;
						arr.push(item.id);
						Storage.set({
							key: type +'good',
							value:arr,
						})	
					}
				}else{
					this.doit(type, item);
					let arr = [];
					arr.push(item.id);
					Storage.set({
						key: type +'good',
						value:arr,
					})
				}
			}
		})

    }
    
    //置顶
	onTob = (type, item)=> {
		Http.post({
			url:'/istop/logic/istop.php',
			data:{
				id:item.id,
				type:type,
			},
			success:(res)=>{
				this.getData(()=>{
					Toast.success('置顶成功');
				});
			}
		})	
	}

	/**
	 * 渲染
	 */
	render() {
        let type = '课程'
        let url = '/screen/article/detail?type=class&id='
		// 渲染
		return (
            <CPage>
                <CContent mt='5' mb='50'>

                    {/* 内容循环 */}
                    {this.state.class.map((item,key) => {
                        console.log('')
                        return (

                            <Listunitone
                                is_master = {this.state.is_master}
                                data = {item}
                                onGoodClick={ this.onGoodClick.bind(this, type, item) }
                                shareurl={ url + item.id }
                                onTob={ this.onTob.bind(this, type, item) }
                                is_video = {true}
                            ></Listunitone>

                        )
                    })}

                </CContent>

                {/* 底部 */}
                <CFooter h='40' bg='#fff' x='center' y='center'>
                    <CHand bg='#d2ba58' w='30' h='30' ra='50' x='center' y='center' onClick={this.onNavigate.bind(this,'/screen/add/list?type=class')}>
                        <CSvg icon={Icon.addtwo} w='20' h='20' color='#fff'></CSvg>
                    </CHand>
                </CFooter>
            </CPage>
		);
	}
};